package com.travelsky.login.service;

import com.travelsky.login.domain.Account;
import com.travelsky.login.domain.WeakPasswordException;
import org.springframework.stereotype.Component;

@Component
class PasswordStrengthVerifier implements AccountVerifier{

  private static final String WEAK_PASSWORD = "(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";

  @Override
  public void verify(Account account) {
    if (isWeakPassword(account)) {
      throw new WeakPasswordException("00004", "当前密码为弱密码请先修改后重新登录!");
    }
  }

  private boolean isWeakPassword(Account account) {
    return !account.getPassword().matches(WEAK_PASSWORD);
  }
}
